package com.zj.business.reader.impl;

import com.zj.business.reader.IPdfReader;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.util.ObjectUtils;

import java.io.File;

/**
 * @author zhangbo
 * @program: PDF-helper
 * @ClassName PdfBoxReader
 * @description: $
 * @date 2023-06-05 19:59:01
 */
@Slf4j
public class PdfBoxReader implements IPdfReader {


    @Override
    public String read(String fileName) {
        PDDocument doc = null;
        String contents = null;
        try {
            doc = PDDocument.load(new File("/Users/zhangbo/Desktop/Java/codes/PDF-helper/src/main/resources/pdf/test.pdf"));

            PDFTextStripper pdfStripper = new PDFTextStripper();
            contents = pdfStripper.getText(doc);

            System.out.println(contents);
        } catch (Exception ex) {
            log.error("pdfBox读取失败：" + ex);
        } finally {

            //关闭流操作
            if (!ObjectUtils.isEmpty(doc)) {
                try {
                    doc.close();
                } catch (Exception ex) {
                    log.error("pdfBox关闭失败：" + ex);
                }
            }
        }


        return contents;
    }
}
